package learning.spring.contacts.web.app.controllers;

import learning.spring.contacts.domain.Contact;
import learning.spring.contacts.service.ContactService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: bibro
 * Date: 21.02.14
 * Time: 21:52
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/contacts")
public class ContactController {

    final Logger logger = LoggerFactory.getLogger(ContactController.class);

    @Autowired
    private ContactService contactService;

    @RequestMapping(method = RequestMethod.GET)
    public String list(Model model) {
        logger.info("listing contacts");

        List<Contact> contacts = (List<Contact>) contactService.findAll();
        model.addAttribute("contacts", contacts);

        return "contacts/list";

    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public String show(@PathVariable Long id, Model model) {
        logger.info("Getting single contact element with id - [ " +id+ " ]" );
        Contact contact = contactService.findById(id);
        model.addAttribute("contact", contact);
        return "contacts/list";
    }

}
